package project5;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


//this file takes in the file and checks for the stops words and
//does the pattern matching for all the freuencies of occureences of phrases
public  class HistogramProcessor 
{
	static int numberofFilesinCorpus;
	static boolean oneAtATimeFlag;

	static HashMap<String, PhraseItem> frequencyHashMap = new HashMap<String, PhraseItem>();
	static ArrayList<String> indexWords = new ArrayList<String>();
	static int counter= 0;

	public static ArrayList<String> Histo(ArrayList<String> wordList,int maxNumberofFiles)
	{
		Iterator<String> i = wordList.iterator();
		PhraseItem temp;
		counter++;
		System.out.println("Running a frequency count...  "+counter);
		while(i.hasNext())
		{

			String key = i.next().toString();
			if(frequencyHashMap.containsKey(key))
			{
				temp= (PhraseItem)frequencyHashMap.get(key);
				temp.frequency++;

				frequencyHashMap.put(key, temp);
			}
			else
			{
				PhraseItem new1 = new PhraseItem();
				new1.phrase=key;
				new1.frequency=1;
				frequencyHashMap.put(key,new1);
			}
		}

		if (counter == maxNumberofFiles)
		{
			System.out.println("Reached the end of it :)");
			ArrayList<PhraseItem> mapentries = new ArrayList<PhraseItem>();
			Iterator<Map.Entry<String,PhraseItem>> it = frequencyHashMap.entrySet().iterator();
			//iterate through the entire list and display each item and it's frequency
			while (it.hasNext())
			{
				Map.Entry<String,PhraseItem> e = (Map.Entry<String,PhraseItem>) it.next();
				PhraseItem pi1 = (PhraseItem) (e.getValue());
				mapentries.add(pi1);
			}

			Comparator<PhraseItem> comparator = Collections.reverseOrder();
			Collections.sort(mapentries, comparator);

			Iterator<PhraseItem> finalIt = mapentries.iterator();
			while (finalIt.hasNext())
			{
				PhraseItem tp = (PhraseItem) finalIt.next();
				if(tp.frequency!=1)
					//System.out.println("" + tp.phrase + " " + tp.frequency);
					if(tp.frequency>10&&tp.frequency<500)
					{
						indexWords.add(tp.phrase);
					}
			}
		}

		//now that the words have been indexed it is a good idea to display them before going anywhere else
		System.out.println("The number of words indexed are "+indexWords.size());
		return indexWords;
	}
}